﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Slja.BLL;
using Slja.Lib;


namespace Slja.WEB.Design
{
    public partial class Tree : System.Web.UI.Page
    {
        /// <summary>
        /// 查询状态
        /// </summary>
        enum SearchState
        {
            /// <summary>
            /// 默认视图，无查询条件
            /// </summary>
            Default,
            /// <summary>
            /// 按店名查询
            /// </summary>
            ShopName,
            /// <summary>
            /// 按项目PrjID查询
            /// </summary>
            PrjID
        }
        DataTable _dt = null;
        bool? _Editable = null;
        public bool Editable
        {
            get
            {
                if (!_Editable.HasValue)
                {
                   _Editable = UserHandle.ValidationHandle(RGP_Tag.Edit);
                }
                return _Editable.Value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {            
           // Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "window.parent.changewin();", true);

            string[] sate = { "是", "否" };
            if (!SessionBox.CheckUserSession())
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                UserHandle.InitModule("Design_PrjStatistical");//初始化模块权限
            }
            btnQuery.Click += new EventHandler(btnQuery_Click);
            if (!IsPostBack)
            {
                
                Slja.BLL.PrjDesign dal = new PrjDesign();
                _dt = dal.GetPrjData("");
                //TreeNode root = new TreeNode("2011项目");


                CreateYearNode(tv1, _dt, SearchState.Default);

                //TreeNode root2 = new TreeNode("2012项目");
                //tv1.Nodes.Add(root2);
                //root2.ChildNodes.Add(new TreeNode("北京"));
            }

        }

        void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlw = "1=1";
            string keyType = drpKeyType.SelectedValue;
            string keyWord = tbxKeyword.Text.Trim();
             Slja.BLL.PrjDesign dal = new PrjDesign();
               
            if (!StringHandle.IsSafeSqlString(keyWord)) { return; }
            SearchState state = SearchState.Default;
            if (keyWord != "")
            {
                if (keyType == "SHOPNAME")
                {
                    sqlw += " and Shop like '%" + keyWord + "%'";
                    state = SearchState.ShopName;

                }
                else if ((keyType == "PRJID"))
                {
                    sqlw += " and PrjID like '" + keyWord + "%'";
                    state = SearchState.PrjID;
                }
            }
            else
            {
                sqlw = "";
                state = SearchState.Default;
            }
            
            _dt = dal.GetPrjData(sqlw);
            CreateYearNode(tv1, _dt, state);
        }
        private void CreateYearNode(TreeView tv, DataTable srcData,SearchState state)
        {
            //情况结点
            tv.Nodes.Clear();
            DataView dv = srcData.DefaultView;
            dv.Sort = "Year asc";
            DataTable dtYear = dv.ToTable(true, "Year");
            string year = "";
            for (int i = 0; i < dtYear.Rows.Count; i++)
            {
                //添加年份
                year = dtYear.Rows[i]["Year"].ToString();
                TreeNode root = new TreeNode(year+"年项目", year);
                tv.Nodes.Add(root);
                if (state == SearchState.Default)
                {
                    if (year == DateTime.Today.Year.ToString())
                    {
                        root.Expanded = true;
                    }
                    else
                    {
                        root.Expanded = false;
                    }
                }
                else
                {
                    root.Expanded = true;
                }
                //添加区域
                DataView dv1 = srcData.DefaultView;
                dv1.RowFilter = "Year='"+year+"'";
                DataTable dtRegion = dv1.ToTable(true,"Region");
                CreateRegionNodes(root, dtRegion,state);
            }
        }

        private void CreateRegionNodes(TreeNode pn, DataTable dtRegion, SearchState state)
        {
            for (int i = 0; i < dtRegion.Rows.Count; i++)
            {
                string region = dtRegion.Rows[i]["region"].ToString();
                TreeNode ndRegion = new TreeNode(region);
               // ndRegion.NavigateUrl = "javascript:void(0);";
                 pn.ChildNodes.Add(ndRegion);
                if (state == SearchState.Default)
                {
                    ndRegion.PopulateOnDemand = true;                   
                }
                else
                {
                    ndRegion.PopulateOnDemand = false;
                    ndRegion.Expanded = true;
                    
                    _dt.DefaultView.RowFilter = "year='" +pn.Value + "' and region='" + ndRegion.Text + "'";
                    DataTable dtShop = _dt.DefaultView.ToTable(true, "Shop");
                    CreateShopNodes(ndRegion, dtShop, state);
                }
            }
            if (Editable)
            {
                TreeNode ndAdd = new TreeNode("[+]添加", "0");
                ndAdd.NavigateUrl = "Content.aspx?year=" + pn.Value + "&id=0&region=0&shop=0&prjid=0";
                ndAdd.Target = "fraContent";
                pn.ChildNodes.Add(ndAdd);
            }
        }
        private void CreateShopNodes(TreeNode pn, DataTable dtShop, SearchState state)
        {
            for (int i = 0; i < dtShop.Rows.Count; i++)
            {
                string shop = dtShop.Rows[i]["shop"].ToString();
                TreeNode ndshop = new TreeNode(shop);
               
                ndshop.SelectAction = TreeNodeSelectAction.Expand;
                ndshop.ToolTip = shop;
                pn.ChildNodes.Add(ndshop);
                if (state == SearchState.Default || state == SearchState.ShopName)
                {
                    ndshop.PopulateOnDemand = true;
                    ndshop.Expanded = false;
                }
                else
                {
                    ndshop.PopulateOnDemand = false;
                    ndshop.Expanded = true;
                    string valuePath = pn.ValuePath;
                    string[] pathArr = valuePath.Split('/');
                    _dt.DefaultView.RowFilter = "Year='" + pathArr[0] + "' and region='" + pathArr[1] + "' and Shop='" + shop + "'";
                    DataTable dtPrj = _dt.DefaultView.ToTable(true, new string[] {"ID","PrjId" ,"PrjName"});
                    CreatePrjNodes(ndshop, dtPrj, state);
                }
            }
            if (Editable)
            {
                string[] pathArr = pn.ValuePath.Split('/');
                TreeNode ndAdd = new TreeNode("[+]添加", "0");
                ndAdd.NavigateUrl = "Content.aspx?year=" + pathArr[0] + "&id=0&region=" + pn.Text + "&shop=0&prjid=0";
                ndAdd.Target = "fraContent";
                pn.ChildNodes.Add(ndAdd);
            }
        }
        private void CreatePrjNodes(TreeNode pn, DataTable dtPrj, SearchState state)
        {
            string valuePath = pn.ValuePath;
            string[] pathArr = valuePath.Split('/');
            for (int i = 0; i < dtPrj.Rows.Count; i++)
            {
                string PrjName = dtPrj.Rows[i]["PrjName"].ToString();
                string PrjID = dtPrj.Rows[i]["PrjId"].ToString();
                string ID = dtPrj.Rows[i]["ID"].ToString();

                TreeNode ndprj = new TreeNode(PrjID, PrjName);
                ndprj.NavigateUrl = "Content.aspx?id=" + ID + "&year=" + pathArr[pathArr.Length - 3] + "&region=" + pathArr[pathArr.Length - 2] + "&shop=" + pn.Text + "&prjid=" + PrjID + "&prjname=" + PrjName;
                ndprj.Target = "fraContent";
                ndprj.ToolTip = PrjName;
                pn.ChildNodes.Add(ndprj);
            }
            if (Editable)
            {
                TreeNode ndAdd = new TreeNode("[+]添加", "0");
                ndAdd.Target = "fraContent";
                ndAdd.NavigateUrl = "Content.aspx?id=0&year=" + pathArr[pathArr.Length - 3] + "&region=" + pathArr[pathArr.Length - 2] + "&shop=" + pn.Text + "&prjid=0";
                pn.ChildNodes.Add(ndAdd);
            }
        }
        protected void tv1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {           
                PrjDesign bll = new PrjDesign();
                DataTable dt = bll.GetPrjData("");
                TreeNode pn = e.Node;
                string valuePath = pn.ValuePath;
                string[] pathArr = valuePath.Split('/');
                if (pathArr.Length == 3)//展现商店下的“项目列表”
                {
                    dt.DefaultView.RowFilter = "Year='" + pathArr[pathArr.Length - 3] + "' and region='" + pathArr[pathArr.Length - 2] + "' and Shop='" + pn.Text + "'";
                    DataTable dtPrj = dt.DefaultView.ToTable();
                    if (e.Node.ChildNodes.Count == 0)
                    {
                        CreatePrjNodes(e.Node, dtPrj, SearchState.Default);
                        //for (int i = 0; i < dtPrj.Rows.Count; i++)
                        //{
                        //    string PrjName = dtPrj.Rows[i]["PrjName"].ToString();
                        //    string PrjID = dtPrj.Rows[i]["PrjId"].ToString();
                        //    string ID = dtPrj.Rows[i]["ID"].ToString();
                           
                        //    TreeNode ndprj = new TreeNode(PrjID, PrjName);
                        //    ndprj.NavigateUrl = "Content.aspx?id=" + ID + "&year=" + pathArr[pathArr.Length - 3] + "&region=" + pathArr[pathArr.Length - 2] + "&shop=" + pn.Text + "&prjid=" + PrjID + "&prjname=" + PrjName;
                        //    ndprj.Target = "fraContent";
                        //    ndprj.ToolTip = PrjName;
                        //    pn.ChildNodes.Add(ndprj);
                        //}
                        //if (_Editable)
                        //{
                        //    TreeNode ndAdd = new TreeNode("[+]添加", "0");
                        //    ndAdd.Target = "fraContent";
                        //    ndAdd.NavigateUrl = "Content.aspx?id=0&year=" + pathArr[pathArr.Length - 3] + "&region=" + pathArr[pathArr.Length - 2] + "&shop=" + pn.Text + "&prjid=0";
                        //    pn.ChildNodes.Add(ndAdd);
                        //}
                    }

                }
                else if (pathArr.Length == 2)//展现区域下面的“商店列表”
                {
                    dt.DefaultView.RowFilter = "year='" + pathArr[pathArr.Length - 2] + "' and region='" + pn.Text + "'";
                    DataTable dtShop = dt.DefaultView.ToTable(true, new string[] {"Shop"});
                    CreateShopNodes(pn, dtShop,SearchState.Default);                    
                }
        }


    }
}